# -*- coding: utf-8 -*-
#--------------------------------------------------------------------------
# Copyright (c) 2009 Barry Schwartz
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
# 
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.

#--------------------------------------------------------------------------

import copy
import glyph_name
import os
import re
import kt
import traceback

from kt import kpathsea

#--------------------------------------------------------------------------

def read_encoding_file(file_name):

    with open(file_name) as f:
        data = f.read()

    # Remove comments and whitespace.
    data = re.compile(r'([^%]*)%.*$', re.MULTILINE).sub('\\1', data)
    data = re.compile(r'(^[ \t]*%.*$|[ \t\f\v\r\n])', re.MULTILINE).sub('', data)

    # Break the data apart.
    m = re.compile(r'/(.+)\[/(.*)\]').match(data)
    encoding_name = m.group(1)
    glyph_names = re.compile(r'/').split(m.group(2))

    return (encoding_name, glyph_names)

# Look in a map file for the encoding and Type 1 font name
# corresponding to a given tfm name. If the encoding is an empty
# string then you are supposed to use the font's built-in encoding.
def get_encoding_and_font_name(tfm_font_name):
    font_map = get_font_map()
    try:
        (encoding, font) = font_map[tfm_font_name]
    except KeyError:
        (encoding, font) = (None, tfm_font_name)
    if encoding is None:
        encoding = ''
    if font is None:
        font = tfm_font_name
    return (encoding, font)

def get_font_map(map_name = 'dvipdfm', font_map_reference = [{}]):
    # |font_map_reference[0]| behaves like a local static variable.
    if len(font_map_reference[0]) == 0:
        map_file_name = kt.kpathsea.find_resource(map_name, format = kt.kpathsea.kpse_fontmap_format)
        with open(map_file_name, 'r') as f:
            for line in f:
                fields = line.split()
                if fields != [] and fields[0][0] != '%':
                    tfm_font_name = fields[0]
                    type1_font = tfm_font_name
                    if 2 <= len(fields) and fields[1][0] != '-':
                        encoding = fields[1]
#                        if encoding in ('default', 'none'): #<-- FIX/REPORT: Cython 0.12 crashes on this
                        if encoding == 'default' or encoding == 'none':
                            encoding = None
                        if 3 <= len(fields) and fields[2][0] != '-':
                            type1_font = fields[2]
                    else:
                        encoding = None
                    font_map_reference[0][tfm_font_name] = (encoding, type1_font)
    return font_map_reference[0]

#--------------------------------------------------------------------------
